package com.soul.doc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.soul.common.utils.ListUtil;
import com.soul.doc.bean.dto.UploadFileDto;
import com.soul.doc.bean.po.FilePo;
import com.soul.common.bean.vo.FileVo;
import com.soul.doc.mapper.FileMapper;
import com.soul.doc.service.FileService;
import com.soul.common.service.BaseService;
import com.soul.doc.utils.FileUtil;
import com.soul.doc.utils.MinioUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sws
 * @since 2023-11-19 11:05:20
 */
@Service
public class FileServiceImpl extends BaseService<FileMapper, FilePo> implements FileService {

    @Autowired
    private MinioUtil minioUtil;

    // 桶名
    private String bucketName = "files";
    //上传文件
    @Override
    public FileVo uploadFile(UploadFileDto fileDto){
        MultipartFile file = fileDto.getFile();
        String folderName = fileDto.getFolderName();
        // 文件名
        String originalFilename = file.getOriginalFilename();
        String fileName = folderName + "/" + originalFilename;
        String upload = minioUtil.upload(file, bucketName, fileName);
        FilePo filePo = new FilePo();
        filePo.setBucketName(bucketName);
        filePo.setFolderName(folderName);
        filePo.setFileName(originalFilename);
        this.save(filePo);
        FileVo fileVo = this.toFileVo(filePo);
        if (originalFilename != null && originalFilename.contains("mp3")) {
            Integer musicLength = FileUtil.getMusicLength(file);
            fileVo.setMusicLength(musicLength);
        }
        return fileVo;
    }

    private FileVo toFileVo(FilePo filePo){
        FileVo fileVo = BeanUtil.copyProperties(filePo, FileVo.class);
        if (filePo.getFolderName().toLowerCase().contains("pic")){
            // 图片预览
            String filename = ObjectUtil.isNotEmpty(filePo.getFolderName()) ? filePo.getFolderName().concat("/").concat(filePo.getFileName()) : fileVo.getFileName();
            String url = minioUtil.preview(filePo.getBucketName(), filename);
            fileVo.setUrl(url);
        }else {
            String filename = ObjectUtil.isNotEmpty(filePo.getFolderName()) ? filePo.getFolderName().concat("/").concat(filePo.getFileName()) : fileVo.getFileName();
            String fileTemporaryUrl = minioUtil.getFileTemporaryUrl(bucketName, filename);
//            String fileUrl = "/api/doc/file/read/" + filePo.getId();
            fileVo.setFileUrl(fileTemporaryUrl);
        }
        return fileVo;
    }

    @Override
    public FileVo getFileVo(Long id){
        FilePo filePo = this.getById(id);
        return this.toFileVo(filePo);
    }
    @Override
    public List<FileVo> getFileVoByIds(List<Long> ids){
        List<FilePo> filePos = this.listByIds(ids);
        return ListUtil.toValueList(filePos,this::toFileVo);
    }


    @Override
    public List<FileVo> queryFileByIds(Collection<Long> ids){
        List<FilePo> filePos = this.listByIds(ids);
        return ListUtil.toValueList(filePos,this::toFileVo);
    }

    @Override
    public void getMusicFile(HttpServletResponse res,Long fileId){
        FilePo filePo = this.getById(fileId);
        String filename = ObjectUtil.isNotEmpty(filePo.getFolderName()) ? filePo.getFolderName().concat("/").concat(filePo.getFileName()) : filePo.getFileName();
        minioUtil.download(bucketName,filePo.getFolderName(),filename,res);
    }
}
